#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QDebug>
#include <QMouseEvent>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , currentPlayer(Player::Black)
    , gameEnded(false)
{
    ui->setupUi(this);

    // 初始化棋盘状态
    for (int i = 0; i < 15; ++i) {
        for (int j = 0; j < 15; ++j) {
            board[i][j] = Player::None;
        }
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event) {
    QPainter painter(this);
    int gridSize = width() / 15; // 棋盘格子大小
    painter.setPen(Qt::black);

    // 绘制横线
    for (int i = 0; i < 15; ++i) {
        painter.drawLine(0, i * gridSize, width(), i * gridSize);
    }

    // 绘制竖线
    for (int j = 0; j < 15; ++j) {
        painter.drawLine(j * gridSize, 0, j * gridSize, height());
    }

    // 绘制棋子
    for (int i = 0; i < 15; ++i) {
        for (int j = 0; j < 15; ++j) {
            if (board[i][j] == Player::Black) {
                painter.setBrush(Qt::black);
                painter.drawEllipse(j * gridSize, i * gridSize, gridSize, gridSize);
            } else if (board[i][j] == Player::White) {
                painter.setBrush(Qt::white);
                painter.drawEllipse(j * gridSize, i * gridSize, gridSize, gridSize);
            }
        }
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event) {
    if (gameEnded) return;

    int gridSize = width() / 15;
    int row = event->y() / gridSize;
    int col = event->x() / gridSize;

    // 确保鼠标点击的位置在棋盘范围内，并且是空位置
    if (row < 0 || row >= 15 || col < 0 || col >= 15 || board[row][col] != Player::None) return;

    placePiece(row, col);

    if (checkWin(row, col)) {
        qDebug() << "Player" << (currentPlayer == Player::Black ? "Black" : "White") << "wins!";
        gameEnded = true;
    }

    currentPlayer = (currentPlayer == Player::Black) ? Player::White : Player::Black;
    update();
}

void MainWindow::placePiece(int row, int col) {
    board[row][col] = currentPlayer;
}

bool MainWindow::checkWin(int row, int col) {
    // 水平方向
    int count = 1;
    for (int i = col - 1; i >= 0 && board[row][i] == currentPlayer; --i) ++count;
    for (int i = col + 1; i < 15 && board[row][i] == currentPlayer; ++i) ++count;
    if (count >= 5) return true;

    // 竖直方向
    count = 1;
    for (int i = row - 1; i >= 0 && board[i][col] == currentPlayer; --i) ++count;
    for (int i = row + 1; i < 15 && board[i][col] == currentPlayer; ++i) ++count;
    if (count >= 5) return true;

    // 左上到右下方向
    count = 1;
    for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == currentPlayer; --i, --j) ++count;
    for (int i = row + 1, j = col + 1; i < 15 && j < 15 && board[i][j] == currentPlayer; ++i, ++j) ++count;
    if (count >= 5) return true;

    // 左下到右上方向
    count = 1;
    for (int i = row + 1, j = col - 1; i < 15 && j >= 0 && board[i][j] == currentPlayer; ++i, --j) ++count;
    for (int i = row - 1, j = col + 1; i >= 0 && j < 15 && board[i][j] == currentPlayer; --i, ++j) ++count;
    if (count >= 5) return true;

    return false;
}
